package com.bw.user.mvvm.viewmodel;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bw.greendaomoudle.green.DaoSessionUtils;
import com.bw.mvvm_comom.BaseApplication;
import com.bw.mvvm_comom.entity.BaseEntity;
import com.bw.mvvm_comom.filed.UIFiled;
import com.bw.mvvm_comom.mvvm.BaseViewModel;
import com.bw.mvvm_comom.network.load.LoadImage;
import com.bw.user.mvvm.db.DaoMaster;
import com.bw.user.mvvm.db.DaoSession;
import com.bw.user.mvvm.db.UserEntityValues;
import com.bw.user.mvvm.model.UserModel;
import com.bw.user.mvvm.model.entity.UserDaoEntity;
import com.bw.user.mvvm.model.entity.UserEventEntity;
import com.bw.user.mvvm.view.activity.EdtUserActivity;
import com.bw.user.mvvm.view.activity.LoginActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;

public class UserViewModel extends BaseViewModel<UserModel> {
    public ObservableField<String> nickName = new ObservableField<>("登录/注册");
    public ObservableField<String>url = new ObservableField<>("");
    String user_name;
    String user_icon;
    @Override
    public void onCreate() {
        super.onCreate();
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(BaseApplication.getApplication(),"user.db");
        SQLiteDatabase writableDatabase = helper.getWritableDatabase();
        DaoMaster master = new DaoMaster(writableDatabase);
        DaoSession session = master.newSession();
        DaoSessionUtils.getInstance().initDaoSession(session);
        EventBus.getDefault().register(this);


        List<UserEntityValues> list = new UserDaoEntity().selectAll();
        if (list.size()>0) {
            UserEntityValues values = new UserDaoEntity().selectAll().get(0);
            nickName.set(values.user_nick_name);
            url.set(values.user_icon);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void update(UserEventEntity entity){
        if (entity.action.equals("updateUser")) {

            List<UserEntityValues> list = new UserDaoEntity().selectAll();
                for (int i = 0; i < list.size(); i++) {
                    user_name = list.get(i).user_name;
                    user_icon = list.get(i).user_icon;
                }
                nickName.set(user_name+"/"+System.currentTimeMillis());
                url.set(user_icon);
        }


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void result(BaseEntity baseEntity) {

    }

    public void edtUser(){
        HashMap<String,Object> map = new HashMap<>();
        map.put(UIFiled.PAGECLAZZ, EdtUserActivity.class);
        getUiChange().getStartPageObServAble().setValue(map);
    }

    public void login(){
        if (!nickName.get().equals("登录/注册"))
            return;
        HashMap<String,Object> map= new HashMap<>();
        map.put(UIFiled.PAGECLAZZ,LoginActivity.class);
        startPage(map);
    }


    public void userOrder(){
        HashMap<String,Object> map= new HashMap<>();
        map.put(UIFiled.ACTIONTYPE,UIFiled.AROUTERSTART);
        map.put(UIFiled.ACTIONROUTERKEY,"/cargroup/ordergroupactivity");
        Bundle bundle = new Bundle();
        bundle.putInt("index",0);
        map.put(UIFiled.VALUESKEY,bundle);
        getUiChange().getStartPageObServAble().setValue(map);
    }

    public void completeOrder(){
        HashMap<String,Object> map= new HashMap<>();
        map.put(UIFiled.ACTIONTYPE,UIFiled.AROUTERSTART);
        map.put(UIFiled.ACTIONROUTERKEY,"/cargroup/ordergroupactivity");
        Bundle bundle = new Bundle();
        bundle.putInt("index",3);
        map.put(UIFiled.VALUESKEY,bundle);
        getUiChange().getStartPageObServAble().setValue(map);
    }
    public void payOrder(){
        HashMap<String,Object> map= new HashMap<>();
        map.put(UIFiled.ACTIONTYPE,UIFiled.AROUTERSTART);
        map.put(UIFiled.ACTIONROUTERKEY,"/cargroup/ordergroupactivity");
        Bundle bundle = new Bundle();
        bundle.putInt("index",1);
        map.put(UIFiled.VALUESKEY,bundle);
        getUiChange().getStartPageObServAble().setValue(map);

    }
    public void receivingOrder(){
        HashMap<String,Object> map= new HashMap<>();
        map.put(UIFiled.ACTIONTYPE,UIFiled.AROUTERSTART);
        map.put(UIFiled.ACTIONROUTERKEY,"/cargroup/ordergroupactivity");
        Bundle bundle = new Bundle();
        bundle.putInt("index",2);
        map.put(UIFiled.VALUESKEY,bundle);
        getUiChange().getStartPageObServAble().setValue(map);
    }
}
